# -*- coding: utf-8 -*-
# vim: set noet ts=4:
#
# scim-python
#
# Copyright (c) 2007-2008 Huang Peng <shawn.p.huang@gmail.com>
#
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330,
# Boston, MA  02111-1307  USA
#
# $Id: $
#
import sys
import PYUtil
import PYDict

class PinYinParser:
	pinyin_dict = set (PYDict.PINYIN_DICT.keys() + PYDict.SHENGMU_DICT.keys ())

	def __init__ (self):
		pass

	def parse_recursive (self, string):
		l = min (6, len(string))
		if l == 0:
			return []
		p = None
		for i in range (l, 0, -1):
			if string[-i:] in self.pinyin_dict:
				p = PYUtil.PinYinWord (string[-i:])
				break
		if p == None:
			raise Exception ("can not parse '%s'" % string.encode ("utf-8"))
		pys = self.parse_recursive (string[:-i])
		pys.append (p)
		return pys

	def parse (self, string):
		pys = []
		for py in string.split (u"'"):
			pys += self.parse_recursive (py)
		return pys

class ShuangPinParser:
	pinyin_dict = PinYinParser.pinyin_dict
	shuangpin_shengmu_dict = set (PYDict.SHUANGPIN_SHENGMU_DICT.keys())

	def __init__ (self):
		pass

	def parse_shuangpin_recursive (self, pys, string):
		if len (string) == 0:
			return []

		if len (string) == 1:
			try:
				shengmu = PYDict.SHUANGPIN_SHENGMU_DICT[string[0]]
			except:
				raise Exception ("can not parse '%s'" % string.encode ("utf-8"))
			
			return [PYUtil.PinYinWord (shengmu)]

		if not string[0] in self.shuangpin_shengmu_dict:
			raise Exception ("can not parse '%s'" % string.encode ("utf-8"))

		yunmu = PYDict.SHUANGPIN_YUNMU_DICT[string[1]]
		p = None

		for i in range (0, len (yunmu), 1):
			pinyin = PYDict.SHUANGPIN_SHENGMU_DICT[string[0]] + yunmu[i]
			if pinyin in self.pinyin_dict:
				p = PYUtil.PinYinWord (pinyin)
				break
		if p == None:
			raise Exception ("can not parse '%s'" % string.encode ("utf-8"))
		pys.append (p)
		pys = self.parse_shuangpin_recursive (pys, string[2:])
		return pys

	def parse(self, string):
		pys = []
		pys += self.parse_shuangpin_recursive (pys, string)
		return pys


if __name__ == "__main__":
#	parser = PinYinParser ()
	parser = ShuangPinParser ()	
	pys = parser.parse (sys.argv[1])
	print "'".join (map (str, pys))
				
